package org.luxor.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.system.entity.SysMenuEntity;
import org.luxor.system.entity.vo.router.MenuVo;
import org.luxor.system.service.ISysMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单管理
 *
 * @author Mark @date 2020-12-29 10:27:13
 */
@RestController
@RequestMapping("/system/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController extends BaseRestController {

    @Resource
    private ISysMenuService sysMenuService;

    @ApiOperation("菜单列表")
    @PostMapping("/list")
    public List<MenuVo> list(@RequestParam(required = false, defaultValue = "0") Long parentId) {
        return sysMenuService.list(parentId);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "menuId", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{menuId}")
    public R<SysMenuEntity> info(@PathVariable("menuId") Long menuId) {
        SysMenuEntity sysMenu = sysMenuService.getById(menuId);
        return R.ok(sysMenu);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public R<SysMenuEntity> save(@Validated(AddGroup.class) @RequestBody SysMenuEntity sysMenu) {
        sysMenuService.save(sysMenu);
        return R.ok(sysMenu);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public R<SysMenuEntity> update(@Validated(UpdateGroup.class) @RequestBody SysMenuEntity sysMenu) {
        sysMenuService.updateById(sysMenu);
        return R.ok(sysMenu);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        sysMenuService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
